package com.peking.donations.service.impl;

import com.peking.donations.base.security.LoggedUser;
import com.peking.donations.dao.DonationUserDao;
import com.peking.donations.model.DonationUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework. security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.google.common.base.Strings;

/**
 * Created by YGR on 16/2/15.
 */
@Service("customUserDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    DonationUserDao sysUserDao;

    @Override
    public UserDetails loadUserByUsername(String openId) throws UsernameNotFoundException {
        if (Strings.isNullOrEmpty(openId)) {
            return null;
        }
        DonationUser user = (DonationUser) sysUserDao.findByWeixinOpenId(openId);
        if (null == user) {
            return null;
        }

        return new LoggedUser(openId,"",user);
    }
}
